package org.jeecg.modules.wangyue.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.wangyue.constant.DelFlagConstant;
import org.jeecg.modules.wangyue.constant.ProblemFeedbackConstant;
import org.jeecg.modules.wangyue.constant.RoleConstant;
import org.jeecg.modules.wangyue.entity.ProblemFeedback;
import org.jeecg.modules.wangyue.entity.UserRoleInfo;
import org.jeecg.modules.wangyue.model.req.ReqCommonModel;
import org.jeecg.modules.wangyue.service.IProblemFeedbackService;
import org.jeecg.modules.wangyue.service.IUserRoleInfoService;
import org.jeecg.modules.wangyue.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
* @Description: 问题反馈表
* @Author: jeecg-boot
* @Date:   2023-06-30
* @Version: V1.0
*/
@Slf4j
@Api(tags="问题反馈管理")
@RestController
@RequestMapping("/wangyue/sys/problemFeedback")
@Validated
public class ProblemFeedbackManageController extends JeecgController<ProblemFeedback, IProblemFeedbackService> {
    @Autowired
    private IProblemFeedbackService problemFeedbackService;
    @Autowired
    private IUserRoleInfoService userRoleInfoService;

    /**
     * 问题反馈管理-分页查询
     *
     * @param keywords      关键字
     * @param fbType        反馈类型(1-订单问题,2-功能问题,3-账号问题,4-操作问题,5-BUG问题,6-其他问题)
     * @param fbStatus      反馈状态(0-待回复,1-已回复,2-取消反馈)
     * @param beginTime     开始时间
     * @param endTime       结束时间
     * @param pageNo        第几页
     * @param pageSize      每页大小
     * @return
     */
    @AutoLog(value = "问题反馈管理-分页查询")
    @ApiOperation(value="问题反馈管理-分页查询", notes="问题反馈管理-分页查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(@RequestParam(name="keywords", required=false) String keywords,
                                   @RequestParam(name="fbType", required=false) String fbType,
                                   @RequestParam(name="fbStatus", required=false) String fbStatus,
                                   @RequestParam(name="beginTime", required = false) String beginTime,
                                   @RequestParam(name="endTime", required = false) String endTime,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
        QueryWrapper<ProblemFeedback> queryWrapper = new QueryWrapper<>();
        if(oConvertUtils.isNotEmpty(keywords)){
            queryWrapper.like("fb_content", keywords);
        }
        if(oConvertUtils.isNotEmpty(fbType)){
            queryWrapper.eq("fb_type", fbType);
        }
        if(oConvertUtils.isNotEmpty(fbStatus)){
            queryWrapper.eq("fb_status", fbStatus);
        }
        if(oConvertUtils.isNotEmpty(beginTime) && oConvertUtils.isNotEmpty(endTime)){
            queryWrapper.apply("create_time >= '" + beginTime + "' and create_time <= '" + endTime + "'");
        }
        queryWrapper.eq("del_flag", DelFlagConstant.normal);
        queryWrapper.orderByAsc("fb_status").orderByDesc("create_time");
        Page<ProblemFeedback> page = new Page<ProblemFeedback>(pageNo, pageSize);
        IPage<ProblemFeedback> pageList = problemFeedbackService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    /**
     * 问题反馈管理-处理
     *
     * @param problemFeedback
     * @return
     */
    @AutoLog(value = "问题反馈管理-处理")
    @ApiOperation(value="问题反馈管理-处理", notes="问题反馈管理-处理")
    @PostMapping(value = "/handleProblem")
    public Result<?> handleProblem(@RequestBody ProblemFeedback problemFeedback) {
        return problemFeedbackService.handleProblem(problemFeedback);
    }


    /**
     *
     * @param userId
     * @param userType
     * @return
     */
    @AutoLog(value = "问题反馈管理-获取用户名或真实姓名")
    @ApiOperation(value="问题反馈管理-获取用户名或真实姓名", notes="问题反馈管理-获取用户名或真实姓名")
    @GetMapping(value = "/getName")
    public Result<?> getName(@RequestParam(name="userId", required=true) @NotBlank(message = "用户id不能为空") String userId,
                                 @RequestParam(name="userType", required=true) @NotBlank(message = "用户类型不能为空") String userType){
        QueryWrapper<UserRoleInfo> userRoleInfoQueryWrapper = new QueryWrapper<>();
        userRoleInfoQueryWrapper.eq("user_id", userId);
        userRoleInfoQueryWrapper.eq("user_type", userType);
        userRoleInfoQueryWrapper.eq("del_flag", DelFlagConstant.normal);
        UserRoleInfo userRoleInfo = userRoleInfoService.getOne(userRoleInfoQueryWrapper);
        if(oConvertUtils.isNotEmpty(userRoleInfo)){
            JSONObject jsonObject = new JSONObject();
            if(RoleConstant.yonghu.equals(userType)){
                jsonObject.put("name", userRoleInfo.getUserName());
            }else if(RoleConstant.jishi.equals(userType)){
                jsonObject.put("name", userRoleInfo.getRealName());
            }
            return Result.OK("获取用户名或真实姓名-成功", jsonObject);
        }else{
            return Result.error("获取用户名或真实姓名-失败");
        }
    }




}
